package com.moodbox.server.mood.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moodbox.server.mood.entity.Mood;
import com.moodbox.server.mood.service.MoodService;
import com.moodbox.server.mood.service.UploadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
public class MoodController {

    private final MoodService moodService;
    private final UploadService uploadService;

    @PostMapping("/mood")
    public Mood createMood(
        HttpServletRequest request,
        @RequestParam String textContent,
        @RequestParam(required = false) MultipartFile image,
        @RequestParam(required = false) MultipartFile audio,
        @RequestParam(required = false) String emotionTag,
        @RequestParam(required = false, defaultValue = "5") Integer emotionIntensity
    ) {
        String userId = (String) request.getAttribute("userId");
        log.info("创建心情记录，文字内容：{}", textContent);
        return moodService.createMood(
            userId, textContent, image, audio, emotionTag, emotionIntensity
        );
    }

    @GetMapping("/mood/today")
    public Mood getTodayMood(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return moodService.getTodayMood(userId);
    }

    @GetMapping("/mood/history")
    public Page<Mood> getMoodHistory(
        HttpServletRequest request,
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        String userId = (String) request.getAttribute("userId");
        return moodService.getMoodHistory(
            userId, 
            new Page<>(page, size)
        );
    }

    @DeleteMapping("/mood/{id}")
    public boolean deleteMood(
        HttpServletRequest request,
        @PathVariable String id
    ) {
        String userId = (String) request.getAttribute("userId");
        log.info("删除心情记录，ID：{}", id);
        return moodService.deleteMood(userId, id);
    }

    @GetMapping("/mood/week-report")
    public String getWeekReport(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        log.info("获取周报，用户ID：{}", userId);
        return moodService.getWeekReport(userId);
    }

    /**
     * 上传图片
     * @param file 图片文件
     * @return 图片URL
     */
    @PostMapping("/upload/image")
    public ResponseEntity<Map<String, String>> uploadImage(
        @RequestParam("file") MultipartFile file,
        HttpServletRequest request
    ) {
        String userId = (String) request.getAttribute("userId");
        log.info("用户{}上传图片", userId);
        
        String imageUrl = uploadService.uploadImage(file);
        
        Map<String, String> response = new HashMap<>();
        response.put("url", imageUrl);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 上传语音
     * @param file 语音文件
     * @return 语音URL
     */
    @PostMapping("/upload/audio")
    public ResponseEntity<Map<String, String>> uploadAudio(
        @RequestParam("file") MultipartFile file,
        HttpServletRequest request
    ) {
        String userId = (String) request.getAttribute("userId");
        log.info("用户{}上传语音", userId);
        
        String audioUrl = uploadService.uploadAudio(file);
        
        Map<String, String> response = new HashMap<>();
        response.put("url", audioUrl);
        
        return ResponseEntity.ok(response);
    }
} 